# Maintainer: Alexey Pavlov <alexpux@gmail.com>

pkgbase=glib2
pkgname=(glib2 glib2-devel glib2-docs)
pkgver=2.66.6
pkgrel=1
pkgdesc="Common C routines used by GTK+ and other libs"
license=(LGPL2)
url="https://www.gtk.org/"
arch=('i686' 'x86_64')
makedepends=('docbook-xsl'
             'gamin-devel'
             'gtk-doc'
             'libffi-devel'
             'libiconv-devel'
             'libxslt-devel'
             'pcre-devel'
             'pkg-config'
             'python3'
             'meson'
             'ninja'
             'zlib-devel')
source=(https://download.gnome.org/sources/glib/${pkgver%.*}/glib-$pkgver.tar.xz
        2.38.2-gconvert-cygwin.patch
        2.64-gmodule-cygwin-rebased.patch)
sha256sums=('80fff9c63d2725834328071c42003c311f77f91caf2285195c587c62f5638329'
            '9054ae2ba34a12bcafa7659f9f21badbfa7aa6114657ed993818cd867ba698f4'
            'dc7a6691f1a19c1b5c6765446d39838331126b29cad45594c100c73da737710c')

prepare() {
  cd glib-${pkgver}

  # https://gitlab.gnome.org/GNOME/glib/-/merge_requests/756
  patch -p2 -i ${srcdir}/2.38.2-gconvert-cygwin.patch

  # Unclear what this is about and why it's not upstream..
  # Rebased from cygwin 2.50-gmodule-cygwin.patch
  patch -p1 -i ${srcdir}/2.64-gmodule-cygwin-rebased.patch
}

build() {
  mkdir "${srcdir}"/build && cd "${srcdir}"/build

  meson \
    --buildtype=plain \
     --prefix=/usr \
     -Dfam=true \
     -Dgtk_doc=true \
    "../glib-${pkgver}"

  meson compile

  DESTDIR="${srcdir}/dest" meson install

  pushd ${srcdir}/dest/usr/share/gdb/auto-load/usr/lib > /dev/null
    mv libglib* msys-glib-2.0-0.dll-gdb.py
    mv libgobject* msys-gobject-2.0-0.dll-gdb.py
  popd > /dev/null
}

check() {
  cd "${srcdir}"/build

  meson test
}

package_glib2() {
  depends=('libxslt' 'libpcre' 'libffi' 'libiconv' 'zlib')
  optdepends=('gamin: for gio fam module'
              'python3: for gdbus-codegen and gtester-report')
  options=('!docs' '!emptydirs')
  license=('LGPL')

  mkdir -p ${pkgdir}/usr/{bin,lib,share}
  cp -f ${srcdir}/dest/usr/bin/*.dll ${pkgdir}/usr/bin/
  for file in gapplication gdbus gio-querymodules glib-compile-schemas gobject-query gsettings; do
    cp -f ${srcdir}/dest/usr/bin/${file}.exe ${pkgdir}/usr/bin/
  done

  cp -rf ${srcdir}/dest/usr/lib/gio ${pkgdir}/usr/lib/
  cp -rf ${srcdir}/dest/usr/share/gdb ${pkgdir}/usr/share/

  mkdir -p ${pkgdir}/usr/share/{man,glib-2.0}
  cp -rf ${srcdir}/dest/usr/share/glib-2.0/gdb ${pkgdir}/usr/share/glib-2.0/
  cp -rf ${srcdir}/dest/usr/share/glib-2.0/schemas ${pkgdir}/usr/share/glib-2.0/
  cp -rf ${srcdir}/dest/usr/share/locale ${pkgdir}/usr/share/

  cp -rf ${srcdir}/dest/usr/share/bash-completion ${pkgdir}/usr/share/

  for _i in "${pkgdir}/usr/share/bash-completion/completions/"*; do
    chmod -x "$_i"
  done
}

package_glib2-devel() {
  pkgdesc="glib2 headers and libraries"
  groups=('development')
  depends=("glib2=${pkgver}" 'gettext-devel' 'libffi-devel' 'libiconv-devel' 'pcre-devel' 'zlib-devel')
  options=('emptydirs')

  mkdir -p ${pkgdir}/usr/{bin,lib,share}
  cp -f ${srcdir}/dest/usr/bin/gdbus-codegen ${pkgdir}/usr/bin/
  cp -f ${srcdir}/dest/usr/bin/glib-* ${pkgdir}/usr/bin/
  rm -f ${pkgdir}/usr/bin/glib-compile-schemas*
  cp -f ${srcdir}/dest/usr/bin/gresource* ${pkgdir}/usr/bin/
  cp -f ${srcdir}/dest/usr/bin/gtester* ${pkgdir}/usr/bin/

  cp -rf ${srcdir}/dest/usr/lib/pkgconfig ${pkgdir}/usr/lib/
  cp -f ${srcdir}/dest/usr/lib/*.a ${pkgdir}/usr/lib/
  cp -rf ${srcdir}/dest/usr/lib/glib* ${pkgdir}/usr/lib/

  cp -rf ${srcdir}/dest/usr/include ${pkgdir}/usr/
  cp -rf ${srcdir}/dest/usr/share/aclocal ${pkgdir}/usr/share/
  cp -rf ${srcdir}/dest/usr/share/glib-2.0 ${pkgdir}/usr/share/
  rm -rf ${pkgdir}/usr/share/glib-2.0/gdb
  rm -rf ${pkgdir}/usr/share/glib-2.0/schemas

  python3 -m compileall -d "/usr/share/glib-2.0/codegen" "${pkgdir}/usr/share/glib-2.0/codegen"
  python3 -O -m compileall -d "/usr/share/glib-2.0/codegen" "${pkgdir}/usr/share/glib-2.0/codegen"
}

package_glib2-docs() {
  pkgdesc="Documentation for glib2"
  conflicts=('gobject2-docs')
  replaces=('gobject2-docs')
  license=('custom')
  options=('docs' '!emptydirs')

  mkdir -p ${pkgdir}/usr/share
  cp -rf ${srcdir}/dest/usr/share/gtk-doc ${pkgdir}/usr/share

  cd glib-$pkgver/docs
  install -m755 -d "${pkgdir}/usr/share/licenses/glib2-docs"
  install -m644 reference/COPYING "${pkgdir}/usr/share/licenses/glib2-docs/"
}
